package phone.qyg.com.qyg.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.application.QXApplication;
import com.app.base.tool.BaseActivity;
import com.app.config.ApplicationConfig;
import com.app.utils.CommonUtil;
import com.app.utils.JSONTool;
import com.app.utils.OptionImageUtils;
import com.app.utils.PromptManager;
import com.app.utils.SharePrefUtil;
import com.app.utils.TransformController;
import com.app.view.CircleImageView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.text.MessageFormat;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.About;
import phone.qyg.com.qyg.model.Base;
import phone.qyg.com.qyg.model.UserInfo;
import phone.qyg.com.qyg.utils.AppHttpRequest;
import phone.qyg.com.qyg.utils.AppUpdateUtil;

/**
 * 作者：lblbh on 2017/12/1 18:41
 * 邮箱：lanbuhan@163.com
 * 功能：我的
 */
@ContentView(R.layout.my)
public class MyActivity extends BaseActivity{

    @ViewInject(R.id.back)
    TextView back;

    @ViewInject(R.id.setting)
    TextView setting;

    @ViewInject(R.id.my_login)
    TextView my_login;

    @ViewInject(R.id.my_name)
    TextView my_name;

    @ViewInject(R.id.my_phone)
    TextView my_phone;

    @ViewInject(R.id.my_right_back)
    TextView my_right_back;

    @ViewInject(R.id.subscribe_count)
    TextView subscribe_count;

    @ViewInject(R.id.course_count)
    TextView course_count;

    @ViewInject(R.id.login_rela)
    RelativeLayout login_rela;

    @ViewInject(R.id.venue_rela)
    RelativeLayout venue_rela;

    @ViewInject(R.id.subscribe_rela)
    RelativeLayout subscribe_rela;

    @ViewInject(R.id.course_rela)
    RelativeLayout course_rela;

    @ViewInject(R.id.collect_rela)
    RelativeLayout collect_rela;

    @ViewInject(R.id.notes_rela)
    RelativeLayout notes_rela;

    @ViewInject(R.id.history_rela)
    RelativeLayout history_rela;

    @ViewInject(R.id.opinion_rela)
    RelativeLayout opinion_rela;

    @ViewInject(R.id.about_rela)
    RelativeLayout about_rela;

    @ViewInject(R.id.update_rela)
    RelativeLayout update_rela;

    @ViewInject(R.id.video_rel)
    RelativeLayout video_rel;

    @ViewInject(R.id.zyz_rl)
    RelativeLayout zyz_rl;

    @ViewInject(R.id.img)
    CircleImageView img;

    String appInfo;

    @Override
    public void initData(Bundle savedInstanceState) {

        appInfo();

        setState();
        setLisener();
        isAppLogin();
    }

    private void setLisener() {
        zyz_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Base base = new Base();
                base.title = "志愿者";
                base.url = "http://www.nmgwhy.cn/volunteer/signView.shtml";
                TransformController.transformControllerModel(QXApplication.getContext(), HtmlActivity.class,base);
            }
        });
        video_rel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TransformController.transformControllerModel(QXApplication.getContext(), VideoActivity.class,null);
            }
        });
    }


    private void setState(){
        String appInfo = SharePrefUtil.getString(SharePrefUtil.KEY.APP_USER,"");
        if(isRequestStr(appInfo)){
            UserInfo userInfo = JSONTool.requestJSONClazz(appInfo, UserInfo.class);
            subscribe_count.setText(MessageFormat.format(QXApplication.getContext().getString(R.string.subscribe_act_key),
                    String.valueOf(userInfo.actCount)));
            course_count.setText(MessageFormat.format(QXApplication.getContext().getString(R.string.course_number_key),
                    String.valueOf(userInfo.regCount)));

            my_login.setVisibility(View.GONE);
            login_rela.setVisibility(View.VISIBLE);
            my_name.setText(setAttributeText(userInfo.nick_name));
            my_phone.setText(setAttributeText(userInfo.mobile));
            setImgPath(userInfo.fixImgUrl,img, OptionImageUtils.getPersonIcon());
        }else{
            subscribe_count.setText(MessageFormat.format(QXApplication.getContext().getString(R.string.subscribe_act_key),"0"));
            course_count.setText(MessageFormat.format(QXApplication.getContext().getString(R.string.course_number_key),"0"));
            my_login.setVisibility(View.VISIBLE);
            login_rela.setVisibility(View.GONE);
            img.setImageResource(R.drawable.my_head);
        }
    }

    private void isAppLogin(){
        LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ApplicationConfig.APP_QX_LOGIN);
        BroadcastReceiver mItemViewListClickReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
//                intent.putExtra(TransformController.ChangeKEY.INTENT_KEY,result);
//                String result = intent.getStringExtra(TransformController.ChangeKEY.INTENT_KEY);
                setState();
            }
        };
        broadcastManager.registerReceiver(mItemViewListClickReceiver, intentFilter);
    }

    @Event(value = {R.id.back,R.id.setting,R.id.my_login,R.id.my_right_back,
                    R.id.subscribe_rela,R.id.course_rela,R.id.collect_rela,
                    R.id.notes_rela,R.id.history_rela,R.id.opinion_rela,
                    R.id.about_rela,R.id.update_rela,R.id.venue_rela})
    private void viewOnClick(View v){

        String userInfo = SharePrefUtil.getString(SharePrefUtil.KEY.APP_USER, "");
        if (!isRequestStr(userInfo) && (!(v == setting || v == about_rela ||
                                          v == back || v == my_login ||
                                          v == update_rela))) {
            //进行拦截 没有登录
            TransformController.transformController(QXApplication.getContext(), LoginActivity.class);
            return;
        }
        Class clazz = null;
        switch (v.getId()){
            case R.id.back:
                this.finish();
                break;
            case R.id.setting:
                clazz = SettingActivity.class;
                break;
            case R.id.my_login:
                clazz = LoginActivity.class;
                break;
            case R.id.my_right_back:
                clazz = UpdateInfoActivity.class;
                break;
            case R.id.subscribe_rela:
                //预约活动
                clazz = MyActivityListActivity.class;
                break;
            case R.id.course_rela:
               //课程
                clazz = MyCourseListActivity.class;
                break;
            case R.id.collect_rela:
               //收藏
                clazz = MyCollectListActivity.class;
                break;
            case R.id.notes_rela:
               //笔记
                clazz = MyNoteListActivity.class;
                break;
            case R.id.history_rela:
               //消息
                clazz = MsgActivity.class;
                break;
            case R.id.opinion_rela:
               //意见
                clazz = OpinionActivity.class;
                break;
            case R.id.about_rela:
               //关于
                String url = JSONTool.jsonDefaluTranString(appInfo, JSONTool.Enum.ABOUT_US);
                About about = new About(setAttributeText(url),
                        QXApplication.getContext().getResources().getString(R.string.my_about));
                TransformController.transformControllerModel(QXApplication.getContext(),HtmlActivity.class,about);
                break;
            case R.id.update_rela:
               //升级
                String appInfoValue = SharePrefUtil.getString(SharePrefUtil.KEY.APP_INFO,"");

               if(isRequestStr(appInfoValue)){
                   String versionValue = JSONTool.jsonDefaluTranString(appInfoValue,JSONTool.Enum.APP_VERSION);
                   String lineVersion = JSONTool.requestJSONfindName(versionValue,JSONTool.Enum.LINE_VERSION);
                   if(CommonUtil.isRequestStr(lineVersion)) {
                       String appVersion = CommonUtil.getVersion();
                       String lochAppVersion = appVersion.replace(".","0");
                       String lineAppVersion = lineVersion.replace(".","0");
                       if(Integer.valueOf(lineAppVersion) > Integer.valueOf(lochAppVersion)){
                           AppUpdateUtil.appUpdate("",back);
                       }else{
                           PromptManager.showToast(R.string.app_version_hint);
                       }
                   }
               }
                break;
            case R.id.venue_rela:
                //场馆
                clazz = MyVenueListActivity.class;
                break;

        }

        if(clazz != null)
            TransformController.transformController(QXApplication.getContext(),clazz);
    }

    @Override
    public void initSucessView() {}

    private void appInfo(){
        appInfo = SharePrefUtil.getString(SharePrefUtil.KEY.APP_INFO,"");

        if(!CommonUtil.isRequestStr(appInfo)){
            AppHttpRequest.httpAppInfo(new AppHttpRequest.OnAppHttpListener() {
                @Override
                public void onAppHttpState(boolean isState, String result) {
                    if(isState){
                        appInfo = result;
                    }
                }
            });
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        String appInfo = SharePrefUtil.getString(SharePrefUtil.KEY.APP_USER,"");
        if(isRequestStr(appInfo)){
            AppHttpRequest.getUserInfo(new AppHttpRequest.OnAppHttpListener() {
                @Override
                public void onAppHttpState(boolean isState, String result) {
                    if(isState && JSONTool.isStatus(result)){
                        String dataValue = JSONTool.requestJSONStringKey(result, JSONTool.Enum.DATA);
                        SharePrefUtil.saveString(SharePrefUtil.KEY.APP_USER, dataValue);
                        UserInfo userInfo = JSONTool.requestJSONClazz(dataValue, UserInfo.class);
                        subscribe_count.setText(MessageFormat.format(QXApplication.getContext().getString(R.string.subscribe_act_key),
                                String.valueOf(userInfo.actCount)));
                        course_count.setText(MessageFormat.format(QXApplication.getContext().getString(R.string.course_number_key),
                                String.valueOf(userInfo.regCount)));

                        my_login.setVisibility(View.GONE);
                        login_rela.setVisibility(View.VISIBLE);
                        my_name.setText(setAttributeText(userInfo.nick_name));
                        my_phone.setText(setAttributeText(userInfo.mobile));
                        setImgPath(userInfo.fixImgUrl,img, OptionImageUtils.getPersonIcon());
                    }
                }
            });
        }
    }

    @Override
    public void initViewHandler() {}
}
